package com.tuinui.trade.entry;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kaweepapkongkittisan
 * Date: 5/25/13
 * Time: 10:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class EntityMapper {

    public Entity convertStockPriceDailyEntryToEntity(StockTradeDailyEntry price){
        Key key = KeyFactory.createKey("Stock", price.getSymbol());

        Entity entity = new Entity("Stock", key);

        entity.setProperty("symbol", price.getSymbol());
        entity.setProperty("flag", price.getFlag());
        entity.setProperty("date", price.getDate());
        entity.setProperty("open", convertValue(price.getOpen()));
        entity.setProperty("high", convertValue(price.getHigh()));
        entity.setProperty("low", convertValue(price.getLow()));
        entity.setProperty("last", convertValue(price.getLast()));
        entity.setProperty("change", convertValue(price.getChange()));
        entity.setProperty("percentChange", convertValue(price.getPercentChange()));
        entity.setProperty("volumeTrade", convertValue(price.getVolumeTrade()));
        entity.setProperty("valueTrade", convertValue(price.getValueTrade()));

        return entity;
    }

    public StockTradeDailyEntry convertEntityToStockPriceDailyEntry(Entity entity){
        StockTradeDailyEntry price = new StockTradeDailyEntry();
        price.setSymbol((String)entity.getProperty("symbol"));
        price.setFlag((String) entity.getProperty("flag"));
        price.setOpen(convertValueToBigDecimal(entity.getProperty("open")));
        price.setHigh(convertValueToBigDecimal(entity.getProperty("high")));
        price.setLow(convertValueToBigDecimal(entity.getProperty("low")));
        price.setLast(convertValueToBigDecimal(entity.getProperty("last")));
        price.setChange(convertValueToBigDecimal(entity.getProperty("change")));
        price.setPercentChange(convertValueToBigDecimal(entity.getProperty("percentChange")));
        price.setVolumeTrade(convertValueToBigDecimal(entity.getProperty("volumeTrade")));
        price.setValueTrade(convertValueToBigDecimal(entity.getProperty("valueTrade")));

        return price;
    }

    public Entity convertPositionEntryToEntity(PositionEntry position){


        Entity entity = new Entity("Position");

        entity.setProperty("date", position.getDate());
        entity.setProperty("symbol", position.getSymbol());
        entity.setProperty("owner", position.getOwner());
        entity.setProperty("purchasedPrice", convertValue(position.getPurchasedPrice()));
        entity.setProperty("volume", convertValue(position.getVolume()));
        entity.setProperty("transactionId", position.getTransactionId());

        return entity;
    }

    public PositionEntry convertEntityToPositionEntry(Entity entity){
        PositionEntry position = new PositionEntry();
        position.setSymbol((String)entity.getProperty("symbol"));
        position.setDate((Date) entity.getProperty("date"));
        position.setOwner((String) entity.getProperty("owner"));
        position.setPurchasedPrice(convertValueToBigDecimal(entity.getProperty("purchasedPrice")));
        position.setVolume(convertValueToBigDecimal(entity.getProperty("volume")));
        position.setId(entity.getKey().getId());
        position.setTransactionId(convertValueToLong(entity.getProperty("transactionId")));
        return position;
    }

    public TransactionEntry convertEntityToTransactionEntry(Entity entity){
        TransactionEntry transaction = new TransactionEntry();
        transaction.setAction((String) entity.getProperty("action"));

        transaction.setSymbol((String)entity.getProperty("symbol"));
        transaction.setDate((Date) entity.getProperty("date"));
        transaction.setOwner((String) entity.getProperty("owner"));
        transaction.setPrice(convertValueToBigDecimal(entity.getProperty("price")));
        transaction.setVolume(convertValueToBigDecimal(entity.getProperty("volume")));
        transaction.setId(entity.getKey().getId());
        transaction.setOriginalTransaction((long)entity.getProperty("originalTransaction"));
        return transaction;
    }

    public Entity convertTransactionEntryToEntity(TransactionEntry transaction){
        Entity entity = new Entity("Transaction");
        entity.setProperty("symbol", transaction.getSymbol());
        entity.setProperty("date", transaction.getDate());
        entity.setProperty("owner", transaction.getOwner());
        entity.setProperty("action", transaction.getAction());
        entity.setProperty("price", convertValue(transaction.getPrice()));
        entity.setProperty("volume", convertValue(transaction.getVolume()));
        entity.setProperty("originalTransaction", transaction.getOriginalTransaction());
        return entity;
    }

    public List<PositionEntry> convertEntityToPositionEntry(List<Entity> entities){
        List<PositionEntry> positions = new ArrayList<PositionEntry>();
        for(Entity entity: entities){
            PositionEntry p = convertEntityToPositionEntry(entity);
            positions.add(p);
        }
        return positions;
    }



    public Object convertValue(BigDecimal decimal){

        if (decimal != null){
            return decimal.doubleValue();
        }else{
            return null;
        }
    }
    public BigDecimal convertValueToBigDecimal(Object decimal){
        if ((decimal != null) && (decimal instanceof Double)){
            if (decimal != null){
                return new BigDecimal((double)decimal);
            }else{
                return null;
            }
        } else{
            return null;
        }
    }
    public long convertValueToLong(Object decimal){
        if ((decimal != null) && (decimal instanceof Long)){
            if (decimal != null){
                return (long)decimal;
            }else{
                return 0;
            }
        } else{
            return 0;
        }
    }

    public TransactionEntry convertPositionEntryToTransactionEntry(PositionEntry positionEntry){
        TransactionEntry transactionEntry = new TransactionEntry();
        transactionEntry.setVolume(positionEntry.getVolume());
        transactionEntry.setPrice(positionEntry.getPurchasedPrice());
        transactionEntry.setDate(positionEntry.getDate());
        transactionEntry.setOwner(positionEntry.getOwner());
        transactionEntry.setSymbol(positionEntry.getSymbol());
        return transactionEntry;
    }

}
